package com.victor.controller.app;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.city.CityIntroduce;
import com.victor.entity.collection.Collections;
import com.victor.entity.praise.CityPraise;
import com.victor.service.CityService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;

@RestController
@RequestMapping("/appcity")
public class AppCityController {
	@Autowired
	private CityService service;
	
	/**
	 * 城市详情
	 * @param cityid
	 * @param uid
	 * @param flag
	 * @return
	 */
	@RequestMapping("/citydetails")
	public Msg firstDetails(String cityid,String uid) {
		if (cityid == null || cityid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		CityIntroduce city = service.selCityById(cityid, uid);
		Collections collections = new Collections();
		collections.setTable_name("c_cityintroduce");
		collections.setUid(uid);
		List<Collections> list = service.selIfCollections(collections);
		String[] str = new String[list.size()];
		for (int i = 0; i < list.size(); i++) {
			String pid = list.get(i).getPid();
			int lastIndexOf = pid.lastIndexOf("=");
			String substring = pid.substring(lastIndexOf+1);
			str[i]=substring;
		}
		for (int i = 0; i < str.length; i++) {
			if (str[i].equals(cityid)) {
				city.setIfcollections("1");
				break;
			}
		}
		if (list == null || list.size()<=0) {
			city.setIfcollections("0");
		}
		return Msg.success().add("city", city);
	}
	/**
	 * 点赞  取消点赞城市
	 * @param cityPraise
	 * @return
	 */
	@RequestMapping("/citypraise")
	public Msg cityPraise(CityPraise cityPraise) {
		String[] str = {"uid","cityid"};
		boolean empty = ClassFilter.somePropertyIsEmpty(cityPraise, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		CityPraise selCityPraiseByCityID = service.selCityPraiseByCityID(cityPraise.getCityid(),cityPraise.getUid());
		if (selCityPraiseByCityID == null) {
			boolean insertCityPraise = service.insertCityPraise(cityPraise);
			if (!insertCityPraise) {
				return Msg.fail("添加点赞失败");
			}
			String praiseCount = service.selCityPraiseCount(cityPraise.getCityid());
			return Msg.success().add("return", "点赞成功").add("prasiecount", praiseCount);
		}else {
			boolean delCityPraise = service.delCityPraise(selCityPraiseByCityID.getId());
			if (!delCityPraise) {
				return Msg.fail("删除城市点赞失败");
			}
			String praiseCount = service.selCityPraiseCount(cityPraise.getCityid());
			return Msg.success().add("return", "取消点赞成功").add("prasiecount", praiseCount);
		}
		
	}
	/**
	 * 修改转发数
	 * @param cityid
	 * @return
	 */
	@RequestMapping("/forwardcount")
	public Msg forwardCity(String cityid) {
		if (cityid == null || cityid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		boolean b = service.updateForwardCount(cityid);
		return Msg.judge(b);
		
	}
	/**
	 * 首页城市查询
	 * @param city
	 * @return
	 */
	@RequestMapping("/showcity")
	public Msg firstShow(String city) {
		if (city == null || city.isEmpty()) {
			return Msg.fail("入参失败");
		}
		CityIntroduce selCityByName = service.selCityByName(city);
		return Msg.success().add("info", selCityByName);
	}
	

}
